﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using Waiter.Controler;

namespace Waiter.View
{
    public partial class FrmOtherRevenue : DevComponents.DotNetBar.Office2007Form
    {
        public FrmOtherRevenue()
        {
            InitializeComponent();
        }
        public void fsLoadCmbStaff()
        {
            StaffCtrl staffCtrl = new StaffCtrl();
            List<STAFF> staffList = staffCtrl.fsGetAllStaffs();
            if (staffList == null || staffList.Count == 0)
            {
                return;
            }

            List<ComboboxItem> itemList = (from s in staffList
                                           select new ComboboxItem()
                                           {
                                               Text = s.NAME,
                                               Value = s.CODE
                                           }).ToList();
            this.cmbStaff.ValueMember = CommonConstants.CMB_VALUE;
            this.cmbStaff.DisplayMember = CommonConstants.CMB_TEXT;
            this.cmbStaff.DataSource = itemList;

            CommonCtrl.fsCmbSetSelectedValue(this.cmbStaff, CommonUtils.fsGetOPID());
        }
        private void FrmOtherRevenue_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOtherRevenue.FrmOtherRevenue_Load", "", "");
            loadingFlag = CommonConstants.FLAG_ON;
            this.dateFrom.Value = DateTime.Now.AddMonths(-1);
            this.dateTo.Value = DateTime.Now;
            fsLoadCmbStaff();
            fsLoadDgrMain();
            loadingFlag = CommonConstants.FLAG_OFF;
            CommonCtrl.fsDgrSetSelectedRow(this.dgrMain, 0, 1);
        }

        private void headerItem1_Load(object sender, EventArgs e)
        {
            this.headerItem1.init("DOANH THU", false);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOtherRevenue.btnClose_Click", "", "");
            this.Close();
        }

        bool addFlag = CommonConstants.FLAG_OFF;
        private void btnAdd_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOtherRevenue.btnAdd_Click", "", "");
            addFlag = CommonConstants.FLAG_ON;
            this.txtMoney.Value = 0;
            this.txtNote.Text = string.Empty;
            this.txtMoney.Focus();
        }

        public void fsLoadDgrMain()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOtherRevenue.fsLoadDgrMain", "", "");
            try
            {
                this.dgrMain.Rows.Clear();
                OtherRevenueCtrl otherRevenueCtrl = new OtherRevenueCtrl();
                List<OTHER_REVENUE> dataList = otherRevenueCtrl.fsGetOtherRevenueList(this.dateFrom.Value, this.dateTo.Value);
                if (dataList == null || dataList.Count == 0)
                {
                    return;
                }

                this.dgrMain.Rows.Add(dataList.Count);
                for (int i = 0; i < dataList.Count; i++)
                {
                    this.dgrMain.Rows[i].Cells["colCode"].Value = dataList[i].CODE;
                    this.dgrMain.Rows[i].Cells["colAddDate"].Value = dataList[i].UPD_DATE;
                    this.dgrMain.Rows[i].Cells["colStaff"].Value = CommonUtils.fsGetOPName(dataList[i].OPID);
                    this.dgrMain.Rows[i].Cells["colStaffCode"].Value = dataList[i].OPID;
                    this.dgrMain.Rows[i].Cells["colAmount"].Value = dataList[i].AMOUNT;
                    this.dgrMain.Rows[i].Cells["colNote"].Value = dataList[i].NOTE;
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOtherRevenue.fsLoadDgrMain", "", ex.ToString());
            }
        }

        bool loadingFlag = CommonConstants.FLAG_ON;
        private void dgrMain_SelectionChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOtherRevenue.dgrMain_SelectionChanged", "", "");
            if (loadingFlag)
            {
                return;
            }

            try
            {
                this.txtMoney.Text = this.dgrMain.CurrentRow.Cells["colAmount"].Value.ToString();
                this.txtNote.Text = this.dgrMain.CurrentRow.Cells["colNote"].Value.ToString();
                string staffCode = this.dgrMain.CurrentRow.Cells["colStaffCode"].Value.ToString();
                CommonCtrl.fsCmbSetSelectedValue(this.cmbStaff, staffCode);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOtherRevenue.dgrMain_SelectionChanged", "", ex.ToString());
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOtherRevenue.btnSave_Click", "", "");
            int money = this.txtMoney.Value;
            string note = this.txtNote.Text.Trim();
            if (money == 0)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Bạn chưa nhập số tiền!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            if (note == string.Empty)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Bạn chưa nhập nội dung ghi chú!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            if (this.dgrMain.Rows.Count == 0)
            {
                addFlag = CommonConstants.FLAG_ON;
            }

            int idx = 0;
            OtherRevenueCtrl otherRevenueCtrl = new OtherRevenueCtrl();
            if (addFlag)
            {
                OTHER_REVENUE item = new OTHER_REVENUE();
                item.CODE = CommonUtils.fsCreateNewCode();
                item.AMOUNT = money;
                item.ADD_DATE = DateTime.Now;
                item.CREATE_DATE = DateTime.Now;
                item.DEL_FLG = CommonConstants.FLAG_OFF;
                item.NOTE = note;
                item.OPID = CommonCtrl.fsCmbGetSelectedValue(this.cmbStaff);
                item.UPD_DATE = DateTime.Now;
                ResultStatus rs = otherRevenueCtrl.fsInsertNewItem(item);
                if (rs != ResultStatus.SUCCESS)
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                addFlag = CommonConstants.FLAG_OFF;
                this.loadingFlag = CommonConstants.FLAG_ON;
                this.dgrMain.Rows.Add(1);
                idx = this.dgrMain.Rows.Count - 1;
                this.dgrMain.Rows[idx].Cells["colCode"].Value = item.CODE;
                this.dgrMain.Rows[idx].Cells["colAddDate"].Value = item.UPD_DATE;
                this.dgrMain.Rows[idx].Cells["colStaff"].Value = CommonUtils.fsGetOPName(item.OPID);
                this.dgrMain.Rows[idx].Cells["colStaffCode"].Value = item.OPID;
                this.dgrMain.Rows[idx].Cells["colAmount"].Value = item.AMOUNT;
                this.dgrMain.Rows[idx].Cells["colNote"].Value = item.NOTE;
                this.loadingFlag = CommonConstants.FLAG_OFF;
                CommonCtrl.fsDgrSetSelectedRow(this.dgrMain, idx, 1);
            }
            else
            {
                string code = this.dgrMain.CurrentRow.Cells["colCode"].Value.ToString();
                ResultStatus rs = otherRevenueCtrl.fsUpdateItem(code, money, note, CommonCtrl.fsCmbGetSelectedValue(this.cmbStaff));
                if (rs != ResultStatus.SUCCESS)
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                idx = this.dgrMain.CurrentRow.Index;
                this.dgrMain.Rows[idx].Cells["colAddDate"].Value = DateTime.Now;
                this.dgrMain.Rows[idx].Cells["colStaff"].Value = CommonCtrl.fsCmbGetSelectedText(this.cmbStaff);
                this.dgrMain.Rows[idx].Cells["colStaffCode"].Value = CommonCtrl.fsCmbGetSelectedValue(this.cmbStaff);
                this.dgrMain.Rows[idx].Cells["colAmount"].Value = money;
                this.dgrMain.Rows[idx].Cells["colNote"].Value = note;
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOtherRevenue.btnDelete_Click", "", "");
            if (this.dgrMain.Rows.Count == 0)
            {
                return;
            }

            DialogResult dls = DevComponents.DotNetBar.MessageBoxEx.Show("Bạn có chắc muốn xóa không?!", CommonMessages.MSG_CONFIRM_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dls != System.Windows.Forms.DialogResult.Yes)
            {
                return;
            }

            int idx = this.dgrMain.CurrentRow.Index;
            OtherRevenueCtrl otherRevenueCtrl = new OtherRevenueCtrl();
            ResultStatus rs = otherRevenueCtrl.fsDeleteItem(this.dgrMain.CurrentRow.Cells["colCode"].Value.ToString());
            if (rs != ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.dgrMain.Rows.RemoveAt(idx);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOtherRevenue.btnSearch_Click", "", "");
            loadingFlag = CommonConstants.FLAG_ON;
            fsLoadDgrMain();
            loadingFlag = CommonConstants.FLAG_OFF;
            CommonCtrl.fsDgrSetSelectedRow(this.dgrMain, 0, 1);
        }

    }
}
